package com.zdy.im.user.service.impl;


import com.zdy.im.user.dto.request.ImJoinFriendStatusRequestDto;
import com.zdy.im.user.service.ImJoinFriendService;
import com.zdy.im.user.dto.response.ImJoinFriendResponseDto;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.io.InputStream;


import com.zdy.im.user.entity.ImJoinFriend;
import com.zdy.im.user.dao.ImJoinFriendDao;
import com.zdy.im.user.dto.request.ImJoinFriendRequestDto;

import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.common.enums.ResultEnum;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.util.PageUtils;
import com.sixeco.framework.util.excel.ExcelUtils;






/**
 * @ClassName: ImJoinFriendDao.java
 * @Description: 用户加为好友申请信息记录表的数据交互处理类
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @version V1.0.0
 * <p>
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 * </p>
 */
@Service
public class ImJoinFriendServiceImpl  implements ImJoinFriendService {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ImJoinFriendDao  imJoinFriendDao;



    /**
     * 保存用户加为好友申请信息记录表同时更新数据库和缓存的实现方法
     * @param imJoinFriendRequestDto ImJoinFriendResponseDto
     * @return
     */
    @Override
    public ImJoinFriendResponseDto saveImJoinFriend(ImJoinFriendRequestDto imJoinFriendRequestDto){
        if(imJoinFriendRequestDto== null){
            return null;
        }
        ImJoinFriend imJoinFriend = ImJoinFriend.build().clone(imJoinFriendRequestDto);
        boolean result = imJoinFriendDao.save(imJoinFriend);
        if(!result){
            return null;
        }
        ImJoinFriendResponseDto imJoinFriendResponseDto = ImJoinFriendResponseDto.build().clone(imJoinFriend);
        return imJoinFriendResponseDto;


    }

    @Override
    public boolean saveBatchImJoinFriend(Collection<ImJoinFriendRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() !=  batchSize){
            return false;
        }
        List<ImJoinFriend> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImJoinFriend.build().clone(e)));
        boolean result = imJoinFriendDao.saveBatch(list,batchSize);
        return result;
    }

    @Override
    public boolean saveOrUpdateBatchImJoinFriend(Collection<ImJoinFriendRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImJoinFriend> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImJoinFriend.build().clone(e)));
        boolean result = imJoinFriendDao.saveOrUpdateBatch(list,batchSize);
        return result;
    }

    @Override
    public boolean updateBatchById(Collection<ImJoinFriendRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImJoinFriend> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImJoinFriend.build().clone(e)));
        boolean result =  imJoinFriendDao.updateBatchById(list,batchSize);
        return result;
    }

    /**
     * 更新用户加为好友申请信息记录表同时更新数据库和缓存的实现方法
     * @param imJoinFriendRequestDto  ImJoinFriendResponseDto
     * @return
     */
    @Override
    public boolean updateImJoinFriend(ImJoinFriendRequestDto imJoinFriendRequestDto){

          if(null ==  imJoinFriendRequestDto){
              return false;
          }

        ImJoinFriend imJoinFriend = ImJoinFriend.build().clone(imJoinFriendRequestDto);

        return imJoinFriendDao.updateById(imJoinFriend);


    }

    /**
     * 更新用户加为好友申请信息记录表同时更新数据库和缓存的实现方法
     * @param imJoinFriendRequestDto  ImJoinFriendRequestDto
     * @return
     */
    @Override
    public boolean updateImJoinFriendStatus (ImJoinFriendStatusRequestDto imJoinFriendRequestDto){
        ImJoinFriend imJoinFriend = null;
        if(imJoinFriendRequestDto.getId() > 0) {
            imJoinFriend = imJoinFriendDao.getById(imJoinFriendRequestDto.getId());
        }else {
            imJoinFriend = imJoinFriendDao.getImJoinFriendByUserAndFriendId(
                    imJoinFriendRequestDto.getSendUserId(),imJoinFriendRequestDto.getToId());
        }
        if (imJoinFriend == null){
            return false;
        }
        boolean userStatus = (imJoinFriend.getToId() == imJoinFriendRequestDto.getToId());
        if (!userStatus){
            return false;
        }
        imJoinFriend.setJoinStatus(imJoinFriendRequestDto.getJoinStatus());
        userStatus = imJoinFriendDao.updateById(imJoinFriend);
        return userStatus;
    }




    /**
     * 通过主键ID删除对象信息实现缓存和数据库,同时删除的方法
     * @param  idList
     * @return
     */
    @Override
    public int delImJoinFriendByIds(List<Long> idList){
        boolean result = false;
        if(null == idList){
            return ResultEnum.FAIL.id();
        }
        if( idList.size() == 1) {
            result = imJoinFriendDao.removeById(idList.get(0));
        }else {
            result =  imJoinFriendDao.removeByIds(idList);
        }
        if(result){
            return ResultEnum.SUCCESS.id();
        }
        return ResultEnum.FAIL.id();

    }


    /**
     * 通过主键ID更新对象用户加为好友申请信息记录表实现缓存和数据库更新的方法
     * @param  imJoinFriendId
     * @return
     */
    @Override
    public ImJoinFriendResponseDto getImJoinFriendById(long imJoinFriendId){
        if(imJoinFriendId < 0 ){
            return null;
        }
        ImJoinFriend imJoinFriend =  imJoinFriendDao.getById(imJoinFriendId);
        if(imJoinFriend == null){
            return null;
        }
        ImJoinFriendResponseDto imJoinFriendResponseDto = ImJoinFriendResponseDto.build().clone(imJoinFriend);

        return imJoinFriendResponseDto ;

    }



    /**
    * 查询条件
    */

    public QueryWrapper<ImJoinFriend> checkQueryCondition(BasePage basePage) {
        QueryWrapper<ImJoinFriend> queryWrapper = new QueryWrapper<>();
        ImJoinFriendRequestDto imJoinFriendRequestDto = (ImJoinFriendRequestDto) basePage.getParamObject();

                                                                                                                                                                                                                                                                return queryWrapper;
    }



    /**
     * 通过分页获取ImJoinFriend信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return List<ImJoinFriendResponseDto>
     * @author suven
     * @date 2021-05-26 15:14:07
     */
    @Override
    public List<ImJoinFriendResponseDto> getImJoinFriendByPage(BasePage basePage){
        List<ImJoinFriendResponseDto> resDtoList =  imJoinFriendDao.getImJoinFriendByPage(basePage);
        return resDtoList;


    }
    /**
     * 通过用户id分页获取ImJoinFriend 用户加为好友申请信息记录表信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author suven
     * @date 2021-05-26 15:14:07
     */
    @Override
    public List<ImJoinFriendResponseDto> getImJoinFriendByUserIdPage(BasePage basePage){
        List<ImJoinFriendResponseDto> resDtoList =  imJoinFriendDao.getImJoinFriendByUserIdPage(basePage);
        return resDtoList;
    }

    /**
     * 通过分页获取ImJoinFriend信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author suven
     * @date 2021-05-26 15:14:07
     */
    @Override
    public ResponseResultList<ImJoinFriendResponseDto> getImJoinFriendByNextPage(BasePage basePage){
        List<ImJoinFriendResponseDto>  list = this.getImJoinFriendByPage(basePage);
        if(null == list ){
            list = new ArrayList<>();
        }
        ResponseResultList responseResultList = ResponseResultList.build(list,basePage.getSize());
        return responseResultList;

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage iPage =  new Query<ImJoinFriend>().getPage(params);
        QueryWrapper<ImJoinFriend> queryWrapper = new QueryWrapper<>();
        IPage<ImJoinFriend> page = imJoinFriendDao.page(iPage,queryWrapper);
        return new PageUtils(page);
    }


    /**
     * 保存用户加为好友申请信息记录表同时更新数据库和缓存的实现方法
     * @return
     */
    public ImJoinFriend  setImJoinFriend(){
        ImJoinFriend imJoinFriend = new ImJoinFriend();
        /**
 			//imJoinFriend.setCreateDate (Date createDate);
 			//imJoinFriend.setModifyDate (Date modifyDate);
 			//imJoinFriend.setStatus (int status);
 			//imJoinFriend.setSendUserId (long sendUserId);
 			//imJoinFriend.setSendUserName (String sendUserName);
 			//imJoinFriend.setSendUserImg (String sendUserImg);
 			//imJoinFriend.setGlobalId (long globalId);
 			//imJoinFriend.setContent (String content);
 			//imJoinFriend.setToId (long toId);
 			//imJoinFriend.setToUserName (String toUserName);
 			//imJoinFriend.setToUserImg (String toUserImg);
 			//imJoinFriend.setJoinStatus (int joinStatus);
		**/

        return imJoinFriend;
    }

    @Override
    public boolean saveData(InputStream initialStream) {
        return ExcelUtils.readExcel(initialStream,imJoinFriendDao, ImJoinFriend.class,0);
    }

    /** 通过申请人id 和被申请加为好的人的Id,查看两人是否加为好友
     * @param imJoinFriendRequestDto
     * @return ImJoinFriendResponseDto
     * **/
    @Override
    public ImJoinFriendResponseDto getImJoinFriendByUserAndFriendId(ImJoinFriendStatusRequestDto imJoinFriendRequestDto){
        ImJoinFriend imJoinFriend = imJoinFriendDao.getImJoinFriendByUserAndFriendId(
                imJoinFriendRequestDto.getSendUserId(),imJoinFriendRequestDto.getToId());
        if(imJoinFriend == null){
            return null;
        }
        ImJoinFriendResponseDto dto =  ImJoinFriendResponseDto.build().clone(imJoinFriend);
        return dto;
    }
}
